package com.lagou.service.impl;

import com.lagou.dao.CourseDao;
import com.lagou.domain.Course;
import com.lagou.domain.CourseSection;
import com.lagou.domain.CourseVo;
import com.lagou.domain.Teacher;
import com.lagou.service.CourseService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseDao courseDao;

    @Override
    public List<Course> findAll() {
        List<Course> list = courseDao.findAll();
        return list;
    }

    @Override
    public List<Course> findCourseByCondition(CourseVo courseVo) {
        List<Course> list = courseDao.findCourseByCondition(courseVo);
        return list;
    }

    @Override
    public void saveCourseAndTeacher(CourseVo courseVo) {
        try {
            System.out.println("courseVo : " + courseVo);
            Course course = new Course();
            BeanUtils.copyProperties(course, courseVo);
            //设置更新时间
            Date date = new Date();
            course.setCreateTime(date);
            course.setUpdateTime(date);
            System.out.println(course);
            System.out.println("createTime: "+ course.getCreateTime());
            //保存课程信息
            courseDao.saveCourse(course);
            Teacher teacher = new Teacher();
            BeanUtils.copyProperties(teacher, courseVo);
            teacher.setCourseId(course.getId());
            teacher.setCreateTime(date);
            teacher.setUpdateTime(date);
            teacher.setIsDel(0);
            //保存讲师信息
            courseDao.saveTeacher(teacher);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public CourseVo findCourseById(int id) {
        CourseVo courseVo = courseDao.findCourseById(id);
        return courseVo;
    }

    @Override
    public void updateCourseAndTeacher(CourseVo courseVo) {
        try {
            Course course = new Course();
            BeanUtils.copyProperties(course, courseVo);
            Date date = new Date();
            course.setUpdateTime(date);
            courseDao.updateCourse(course);

            Teacher teacher = new Teacher();
            BeanUtils.copyProperties(teacher, courseVo);
            teacher.setUpdateTime(date);
            teacher.setCourseId(course.getId());
            courseDao.updateTeacher(teacher);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateCourseStatus(int id, int status) {
        Course course = new Course();
        course.setId(id);
        course.setStatus(status);
        course.setUpdateTime(new Date());
        courseDao.updateCourseStatus(course);
    }

    @Override
    public List<CourseSection> findSectionAndLessonByCourseId(int courseId) {
        List<CourseSection> list = courseDao.findSectionAndLessonByCourseId(courseId);
        return list;
    }
}
